// var host = 'http://10.10.0.2:8080/car25';   
// var host = 'http://ipv6.tgxkj.com/car25';
// var host = 'https://2241vm6605or.vicp.fun/car25';
// var host = 'http://192.168.1.6/car25';
var host = 'https://ksmeilv.com/car25';
// var host = 'http://192.168.2.213/car25';

function get(url, data, callback, fail, comp) {
	var newurl = host + url;
	uni.showLoading({
		title: '请稍等'
	})
	uni.request({
		url: newurl,
		header: {
			'token': uni.getStorageSync('token')
		},
		method: 'GET',
		data: data ? data : {},
		success: function(res) {
			if (res.statusCode == 200) {
				callback(res);
			} else if (res.statusCode == 401) {
				uni.reLaunch({
					url: '/pages/login/login'
				})

			} else {
				uni.showModal({
					title: '请求异常',
					content: res.data.message,
					showCancel: false,
					confirmText: '知道了',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			}
		},
		fail: function(res) {
			console.log(newurl);
			console.log("为什么没到这里？")
			console.log(res);
			if (res.statusCode == 401) {
				uni.redirectTo({
					url: '/pages/login/login'
				})

			} else {
				uni.showModal({
					title: '请求异常',
					content: res.data.message,
					showCancel: false,
					confirmText: '知道了',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			}
			fail(res);
		},
		complete: function(res) {
			// console.log(res);
			uni.hideLoading();
		}
	})
}

function post(url, data, callback, fail, comp) {
	var newurl = host + url;
	uni.showLoading({
		title: '请稍等'
	})
	uni.request({
		url: newurl,
		header: {
			'token': uni.getStorageSync('token')
		},
		method: 'POST',
		data: data ? data : {},
		success: function(res) {
			if (res.statusCode == 200) {
				callback(res);
			} else if (res.statusCode == 401) {
				uni.reLaunch({
					url: '/pages/login/login'
				})
			} else {
				uni.showModal({
					title: '请求异常',
					content: res.data.message,
					showCancel: false,
					confirmText: '知道了',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			}
		},
		fail: function(res) {
			if (res.statusCode == 401) {
				uni.reLaunch({
					url: '/pages/login/login'
				})
			} else {
				uni.showModal({
					title: '请求异常',
					content: res.data.message,
					showCancel: false,
					confirmText: '知道了',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			}
			fail(res);
		},
		complete: function(res) {
			uni.hideLoading();
		}
	})
}

function getAuthorization(callback, fail, comp) {
	var newurl = host + '/orders/getCredentials';
	uni.request({
		url: newurl,
		header: {
			'token': uni.getStorageSync('token')
		},
		method: 'GET',
		data: {},
		success: function(res) {
			if (res.statusCode == 200) {} else if (res.statusCode == 401) {
				callback(res);
				uni.reLaunch({
					url: '/pages/login/login'
				});
			} else {
				console.log(res);
				uni.showModal({
					title: '请求异常',
					content: res.errMsg,
					showCancel: false,
					confirmText: '知道了',
					success: res => {},
					fail: () => {},
					complete: () => {}
				});
			}
		},
		fail: function(res) {
			console.log(res);
			uni.showModal({
				title: '请求异常',
				content: res.errMsg,
				showCancel: false,
				confirmText: '知道了',
				success: res => {},
				fail: () => {},
				complete: () => {}
			});
		},
		complete: function(res) {
			// comp(res);
		}
	})
}

module.exports = {
	get: get,
	post: post,
	host: host,
	getAuthorization
}